package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogain;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/detail")
    public Object detail(String id) {
        //根据攻略的id查出该攻略的所有信息
        Travel travel = travelService.get(id);
        //并添加上头像
        travel.setAuthor(userInfoService.get(travel.getUserId()));
        //并添加上目的地
        travel.setDest(destinationService.get(travel.getDestId()));
        //返回
        JsonResult success = JsonResult.success();
        success.setData(travel);
        return success;
    }

    @GetMapping("/query")
    public Object query(String destId, @ModelAttribute("qo") TravelQuery qo) {
        //获得分页对象
        Page<Travel> page = travelService.query(qo);
        JsonResult success = JsonResult.success();
        success.setData(page);
        return success;
    }

    @GetMapping("/input")
    public Object input(String id) {
        //要一个Map集合,集合中装游记对象和目的地对象
        //
        Map<String, Object> map = new HashMap<>();
        //查询出所有目的地
        List<Destination> list = destinationService.selectAll();
        //添加到集合中
        map.put("dests", list);
        //查询出当前游记对象
        Travel travel = travelService.get(id);
        //添加到集合中
        map.put("tv", travel);
        return JsonResult.success(map);
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    //保存方法
    @PostMapping("/saveOrUpdate")       //参数上加注解,说明为解析器生成
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {
        //调用游记服务的更新或保存方法,进行更新保存游记
        travelService.saveOrUpdate(travel,userInfo);
        //将游记的id存入
        return JsonResult.success(travel.getId());
    }


    @GetMapping("/comments")       //参数上加注解,说明为解析器生成
    public Object comments(String travelId) {
        //查询出游记id的所有评论
        //注意评论嵌套评论的情况
        return JsonResult.success(travelCommentService.comments(travelId));
    }

    @RequireLogain
    @PostMapping("/commentAdd")       //参数上加注解,说明为解析器生成
    public Object commentAdd(TravelComment travelComment,@UserParam UserInfo userInfo) {
        //调用服务层的添加方法
        travelCommentService.commentAdd(travelComment,userInfo);
        return JsonResult.success();
    }



}
